package com.newcoin.kline.controller;

import com.newcoin.kline.entity.KlineConfigEntity;
import com.newcoin.kline.entity.MarksEntity;
import com.newcoin.kline.entity.TimescaleMarksEntity;
import com.newcoin.kline.service.KlineService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/kline")
@Slf4j
//@CrossOrigin(origins = "http://175.17.189.88:8080")
public class KlineController {
    @Autowired
    private KlineService service;


    @GetMapping(value = "/config")
    public KlineConfigEntity getConfig() {
        try {
            return service.getConfig();
        } catch (Exception e) {
            log.error(e.getMessage());
            return null;
        }
    }

    @GetMapping(value = "/time")
    public Long getTime() {
        try {
            return System.currentTimeMillis() / 1000L;
        } catch (Exception e) {
            log.error(e.getMessage());
            return System.currentTimeMillis() / 1000L;
        }
    }

    @GetMapping(value = "/symbols")
    public Map<String, Object> getSymbol(String symbol) {
        try {
            return service.getSymbol(symbol);
        } catch (Exception e) {
            log.error(e.getMessage());
            return null;
        }
    }

    @GetMapping(value = "/marks")
    public MarksEntity getMarks(String symbol, Long from, Long to, String resolution) {
        try {
            return service.getMarks(symbol, from, to, resolution);
        } catch (Exception e) {
            log.error(e.getMessage());
            return null;
        }
    }

    @GetMapping(value = "/timescale_marks")
    public List<TimescaleMarksEntity> getTimescaleMarks(String symbol, Long from, Long to, String resolution) {
        try {
            return service.getTimescaleMarks(symbol, from, to, resolution);
        } catch (Exception e) {
            log.error(e.getMessage());
            return null;
        }
    }

    @GetMapping(value = "/history")
    public Map<String, Object> history(String symbol, String resolution, Long from, Long to) {
        try {
            return service.history(symbol, resolution, from, to);
        } catch (Exception e) {
            log.error(e.getMessage());
            return null;
        }
    }

    @GetMapping(value = "/symbol_info")
    public Map<String, Object> symbolInfo(String group) {
        try {
            return service.symbolInfo(group);
        } catch (Exception e) {
            log.error(e.getMessage());
            return null;
        }
    }
}
